package com.sservice.commonBusiness.controller.admin;

import com.github.pagehelper.PageHelper;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.commonBusiness.e.Category;
import com.sservice.commonBusiness.service.CategoryService;
import com.sservice.commonBusiness.type1.Category_TYPE;
import com.sservice.commonBusiness.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Controller
@RequestMapping("/admin/category/")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(Category category) {
        if ("coupons".equals(category.getType1()) || "gift".equals(category.getType1())) {
            category.setEn(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        categoryService.insertSelective(category);
        return new BaseResult(true, category);
    }
    @RequestMapping("listSummary")
    @ResponseBody
    public ListResult listSummary(CategoryVo categoryVo) {
        PageHelper.startPage(categoryVo.getPage(), categoryVo.getSize());
        List<CategoryVo> list = categoryService.querySummaryByPage(categoryVo);
        ListResult listResult = new ListResult(true, list);
        listResult.setPage(new Pagination(categoryVo.getPage(), categoryService.queryCount(categoryVo), categoryVo.getSize()));
        return listResult;
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult delete(Category category) {
        return new BaseResult(true, String.valueOf(categoryService.deleteByPrimaryKey(category.getId())), 200);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(Category category) {
        return new BaseResult(true, String.valueOf(categoryService.updateByPrimaryKeySelective(category)), 200);
    }

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(@RequestParam Integer id) {
        Category category = categoryService.selectByPrimaryKey(id);
        return new BaseResult(true, category);
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(CategoryVo categoryVo) {
        PageHelper.startPage(categoryVo.getPage(), categoryVo.getSize());
        if (categoryVo.getType1() == null) { // 默认类型数据为后台菜单
            categoryVo.setType1(Category_TYPE.TYPE_MENU.getValue());
            categoryVo.setSysId(null);// 菜单客户归属区分
        }
        ;
        List<CategoryVo> list = categoryService.queryByPage(categoryVo);
        ListResult listResult = new ListResult(true, list);
        listResult.setPage(new Pagination(categoryVo.getPage(), categoryService.queryCount(categoryVo), categoryVo.getSize()));
        return listResult;
    }

    @RequestMapping("listDR")
    @ResponseBody
    public ListResult listDR(CategoryVo categoryVo) {
        List<CategoryVo> list = categoryService.queryWithDrByPage(categoryVo);
        ListResult listResult = new ListResult(true, list);
        listResult.setPage(new Pagination(categoryVo.getPage(), categoryService.countWithDrByPage(categoryVo), categoryVo.getSize()));
        return new ListResult(true, listResult);
    }
}
